package org.xukai.spark.day1

import org.apache.spark.{SparkConf, SparkContext}

/**
  * @desc
  * @author xukai
  * @date 2017-11-25-下午 4:59
  */
object Cogoup {


  def main(args: Array[String]) {
    //非常重要，是通向Spark集群的入口
    val conf = new SparkConf().setAppName("WC").setMaster("local")
    val sc = new SparkContext(conf)

    cogroup(sc)
    sc.stop()
  }

  val student = Array(
    Tuple2(1, "hello"),
    Tuple2(2, "joe"),
    Tuple2(3, "uxk"),
    Tuple2(3, "xls")
  )
  val score = Array(
    Tuple2(1, 66),
    Tuple2(2, 77),
    Tuple2(3, 88),
    Tuple2(1, 88)
  )

  def join(sc: SparkContext): Unit ={
    val students = sc.parallelize(student)
    val scores = sc.parallelize(score)
    val result = students.join(scores)
    result.foreach(stu => {
      println(stu._1)
      println(stu._2._1)
      println(stu._2._2)
      println("====================")
    })
  }

  def cogroup(sc: SparkContext): Unit ={
    val students = sc.parallelize(student)
    val scores = sc.parallelize(score)
    val result = students.cogroup(scores)
    result.foreach(stu => {
      println(stu._1)
      println(stu._2._1)
      println(stu._2._2)
      println("====================")
    })
  }






}
